import React, { forwardRef } from 'react'
import { Form, Select, Input } from 'antd'
import PropTypes from 'prop-types'
import AutoNameInput from './AutoNameInput'
import { checkIdCard } from '@js/identityTest'
const { Item } = Form
const { Option } = Select
const { TextArea } = Input
const layout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 19 }
}

// 验证身份证
function validateLicense (formRef, rule, value) {
  const type = formRef.current.getFieldValue(['license_type'])
  if (type === '身份证' && value.length) {
    const e = checkIdCard.test(value)
    if (e) {
      return Promise.reject(new Error(e))
    }
  }
  return Promise.resolve()
}

// 改变当事人
function changeParty (v, type, partyList, formRef) {
  const ids = partyList.map(li => li.id)
  const party = v.map(id => {
    const index = ids.indexOf(id)
    const obj = partyList[index]
    return {
      scope_type: '', // 代理类型
      scope: '', // 代理权限
      type, // 当事人类型
      partyid: obj.id
    }
  })
  formRef.current.setFieldsValue({
    party,
    partyIndex: party.map(p => p.partyid)
  })
}

function Main ({ initialValues, dictionaries, isPre, onFinish, type, caseFormRef, partyList }, ref) {
  const licenseTypeList = (dictionaries['自然人证件类型'] || []).map(li => li.title)
  return (
    <Form ref={ref} {...layout} initialValues={initialValues} onFinish={d => onFinish(d)}>
      <Item name='id' noStyle><span /></Item>
      <Item name='party' noStyle><span /></Item>
      <Item name='party_type' noStyle><span /></Item>
      <Item label={`${type}名称`} name='partyIndex' rules={[{ required: true, message: `${type}不能为空` }]}>
        <Select mode='multiple' onChange={v => changeParty(v, type, partyList, ref)} placeholder={`请选择${type}`}>
          {partyList.map(li => (
            <Option key={li.id} value={li.id}>{li.name}</Option>
          ))}
        </Select>
      </Item>
      <Item label='代理人姓名' name='name' rules={[{ required: true, message: '代理人姓名不能为空' }]}>
        <AutoNameInput name={name} formRef={ref} />
      </Item>
      <Item label='证件类型' name='license_type'>
        <Select
          placeholder='请选择证件类型' onChange={e => {
            ref.current.setFieldsValue({ license_type: e })
            ref.current.validateFields([['license']])
          }}
        >
          {licenseTypeList.map((li, i) => (
            <Option value={li} key={li}>{li}</Option>
          ))}
        </Select>
      </Item>
      <Item label='证件号码' name='license' rules={[{ validator: (...args) => validateLicense(ref, ...args) }]}>
        <Input placeholder='请输入证件号码' />
      </Item>
      <Item label='所在单位' name='structure'>
        <Input placeholder='请输入所在单位' />
      </Item>
      <Item label='地址' name='address' rules={[{ required: true, message: '地址不能为空' }]}>
        <TextArea placeholder='请输入地址' style={{ resize: 'none' }} />
      </Item>
      <Item label='手机号码' name='mobile' rules={[{ pattern: /^1\d{10}$/, message: '手机号码格式不正确' }, { required: true, message: '手机号码不能为空' }]}>
        <Input placeholder='请输入手机号码' maxLength={11} />
      </Item>
      <Item label='邮箱' name='email' rules={[{ type: 'email', message: '邮箱格式不正确' }]}>
        <Input placeholder='请输入邮箱' />
      </Item>
      <Item label='其他联系方式' name='tel'>
        <Input placeholder='请输入其他联系方式' />
      </Item>
    </Form>
  )
}

const RefMain = forwardRef(Main)

RefMain.propTypes = {
  initialValues: PropTypes.object,
  dictionaries: PropTypes.object,
  isPre: PropTypes.bool,
  onFinish: PropTypes.func,
  type: PropTypes.string,
  caseFormRef: PropTypes.object,
  partyList: PropTypes.array
}

RefMain.defaultProps = {
  initialValues: {},
  dictionaries: {},
  isPre: false,
  onFinish: () => {},
  type: '',
  caseFormRef: null,
  partyList: []
}

export default RefMain
